import java.sql.*;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

public class DebugLoginFlow {
    private static final String URL = "jdbc:mysql://db.test1.wcsbapp.com:3306/ChainUp?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
    private static final String USERNAME = "admin";
    private static final String PASSWORD = "0YvoGUzwhumnf2KGQvjN";
    
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            
            System.out.println("=== 调试登录流程 ===");
            System.out.println("1. 测试数据库连接...");
            
            try (Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD)) {
                System.out.println("   ✓ 数据库连接成功");
                
                // 1. 检查sys_user表中的admin用户
                System.out.println("\n2. 查询sys_user表中的admin用户...");
                String userQuery = "SELECT user_id, user_name, nick_name, email, phonenumber, sex, avatar, password, status, del_flag, login_ip, login_date, create_by, create_time, update_by, update_time, remark FROM sys_user WHERE user_name = 'admin' AND del_flag = '0'";
                
                try (PreparedStatement stmt = conn.prepareStatement(userQuery)) {
                    try (ResultSet rs = stmt.executeQuery()) {
                        if (rs.next()) {
                            System.out.println("   ✓ 找到admin用户:");
                            System.out.println("     user_id: " + rs.getLong("user_id"));
                            System.out.println("     user_name: " + rs.getString("user_name"));
                            System.out.println("     nick_name: " + rs.getString("nick_name"));
                            System.out.println("     status: " + rs.getString("status"));
                            System.out.println("     del_flag: " + rs.getString("del_flag"));
                            String dbPassword = rs.getString("password");
                            System.out.println("     password hash: " + dbPassword);
                            
                            // 2. 测试BCrypt密码验证
                            System.out.println("\n3. 测试BCrypt密码验证...");
                            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
                            boolean passwordMatch = encoder.matches("admin123", dbPassword);
                            System.out.println("   密码验证结果: " + (passwordMatch ? "✓ 匹配" : "✗ 不匹配"));
                            
                            // 3. 检查用户角色
                            System.out.println("\n4. 检查用户角色...");
                            String roleQuery = "SELECT r.role_id, r.role_name, r.role_key, r.role_sort, r.status " +
                                             "FROM sys_role r " +
                                             "LEFT JOIN sys_user_role ur ON ur.role_id = r.role_id " +
                                             "WHERE ur.user_id = ? AND r.del_flag = '0'";
                            
                            try (PreparedStatement roleStmt = conn.prepareStatement(roleQuery)) {
                                roleStmt.setLong(1, rs.getLong("user_id"));
                                try (ResultSet roleRs = roleStmt.executeQuery()) {
                                    if (roleRs.next()) {
                                        System.out.println("   ✓ 找到用户角色:");
                                        System.out.println("     role_name: " + roleRs.getString("role_name"));
                                        System.out.println("     role_key: " + roleRs.getString("role_key"));
                                        System.out.println("     status: " + roleRs.getString("status"));
                                    } else {
                                        System.out.println("   ✗ 未找到用户角色");
                                    }
                                }
                            }
                            
                        } else {
                            System.out.println("   ✗ 未找到admin用户");
                        }
                    }
                }
                
                // 4. 检查验证码配置
                System.out.println("\n5. 检查验证码配置...");
                String configQuery = "SELECT config_key, config_value FROM sys_config WHERE config_key = 'sys.account.captchaEnabled'";
                try (PreparedStatement stmt = conn.prepareStatement(configQuery)) {
                    try (ResultSet rs = stmt.executeQuery()) {
                        if (rs.next()) {
                            System.out.println("   验证码配置: " + rs.getString("config_key") + " = " + rs.getString("config_value"));
                        } else {
                            System.out.println("   未找到验证码配置");
                        }
                    }
                }
                
                // 5. 测试完整的登录用户查询（模拟UserDetailsServiceImpl）
                System.out.println("\n6. 模拟UserDetailsServiceImpl查询...");
                String fullUserQuery = "select u.user_id, u.dept_id, u.user_name, u.nick_name, u.email, u.avatar, u.phonenumber, u.password, u.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark, d.dept_name, d.leader from sys_user u left join sys_dept d on u.dept_id = d.dept_id where u.user_name = ?";
                
                try (PreparedStatement stmt = conn.prepareStatement(fullUserQuery)) {
                    stmt.setString(1, "admin");
                    try (ResultSet rs = stmt.executeQuery()) {
                        if (rs.next()) {
                            System.out.println("   ✓ UserDetailsServiceImpl查询成功:");
                            System.out.println("     user_name: " + rs.getString("user_name"));
                            System.out.println("     password: " + rs.getString("password"));
                            System.out.println("     status: " + rs.getString("status"));
                            System.out.println("     del_flag: " + rs.getString("del_flag"));
                            System.out.println("     dept_name: " + rs.getString("dept_name"));
                        } else {
                            System.out.println("   ✗ UserDetailsServiceImpl查询失败");
                        }
                    }
                }
                
            }
            
        } catch (Exception e) {
            System.err.println("错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
}